/** Game data generator */
pepperstad.Generator = function (opts) {

    this.resourceLoader = opts.resourceLoader;

    this.generateSkills = function (specialistChance) {
        var level, skill, skillType, skills = [];
        var skillPool = _.clone(this.resourceLoader.get("skills"));
        skillPool = _.shuffle(skillPool); // randomize pick
        var maxDifficulty = rnd(5, 9);
        skillPool = _.reject(skillPool, function (sk) {
            return sk.difficulty > maxDifficulty;
        });
        var isSpecialist = draw(specialistChance);
        // Specialists gets between 1 skill and 10% of available skills
        // Others gets between 1 skill and 20% of available skills
        var numSkillsToAdd = rnd(1,
                this.resourceLoader.get("skills").length * (isSpecialist ? 0.1 : 0.2));
        for (; numSkillsToAdd > 0; numSkillsToAdd--) {
            skill = new pepperstad.Skill();
            skillType = skillPool.shift();
            skill.name = skillType.name;
            if (isSpecialist) {
                // a "specialist" is just a more skilled person
                level = rnd(5, 9);
                // game balancing: allow only one specialist skill per person
                isSpecialist = false;
            } else {
                level = rnd(1, 5);
            }
            skill.level = level;
            skills.push(skill);
        }
        return skills;
    };
    /**
     * @param def generationDefinition
     */
    this.generatePerson = function (def) {
        var skills = this.generateSkills(def.specialistChance);
        var minStrength = 1,
            maxStrength = 10,
            minAge = 1,
            maxAge = 90;
        if (def.strengthRange) {
            minStrength = def.strengthRange.from;
            maxStrength = def.strengthRange.to;
        }
        if (def.ageRange) {
            minAge = def.ageRange.from;
            maxAge = def.ageRange.to;
        }
        return new pepperstad.Person({
            strength: rnd(minStrength, maxStrength),
            age: rnd(minAge, maxAge),
            name: this.generateName(),
            skills: skills
        });
    };

    /**
     * @param def generationDefinition
     */
    this.generatePeople = function (def) {
        var inhabitants = [];
        for (var i = 0; i < def.number; i++) {
            inhabitants.push(this.generatePerson(def));
        }
        return inhabitants;
    };

    this.generateName = function () {
        return rndElement(this.resourceLoader.get("names")) + " " +
            rndElement(this.resourceLoader.get("surnames"));
    };

};
